package com.simple.action.fee;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.FeeGrant;
import com.simple.service.fee.FeeGrantService;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.core.SessionBean;
import com.xunj.util.Common;

/**
 * 费用预留发放
 * @author 王瑾
 *
 */
public class FeeGrantAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -365972686059686745L;
	private FeeGrant feeGrant; //费用存留


	



	/**
	 * 查询核销完毕的数据
	 * @return
	 * @throws Exception
	 */
	public String listFeeGrant() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		paralist.add(new ParaUnit("state", "在用", ParaUnit.EQ));
		
		if(POST())
		{
			if(!feeGrant.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", feeGrant.getAreaId(), ParaUnit.EQ));
			if(!feeGrant.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", feeGrant.getAreaDeptId(), ParaUnit.EQ));
			String feeDateFrom = request.getParameter("feeDateFrom");
			String feeDateTo = request.getParameter("feeDateTo");
			if(!feeDateFrom.equals(""))
			{
				paralist.add(new ParaUnit("feeGrantDate",Common.newDate(feeDateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
				request.setAttribute("feeDateFrom", feeDateFrom);
			}
			if(!feeDateTo.equals(""))
			{
				paralist.add(new ParaUnit("feeGrantDate",Common.newDate(feeDateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
				request.setAttribute("feeDateTo", feeDateTo);
			}
			if(!feeGrant.getFeeGrantType().equals(""))
				paralist.add(new ParaUnit("feeGrantType", feeGrant.getFeeGrantType(), ParaUnit.EQ));			
			if(!feeGrant.getFeeGrantCompanyName().equals(""))
				paralist.add(new ParaUnit("feeGrantCompanyName", feeGrant.getFeeGrantCompanyName(), ParaUnit.LIKE));			
		}
		List list = dao.criteriaByPage(FeeGrant.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_grant/lst_fee_grant.jsp");
	}
	/**
	 * 读取预留余额
	 * 
	 * @return
	 * @throws Exception
	 */
	public String readAmountFeeGrant() throws Exception {
		
		FeeGrantService fgs = new FeeGrantService(dao);
		Double d = fgs.checkFeeGrantAmount(feeGrant);
		
		request.setAttribute("data", d);
		return toResult();

	}
	/**
	 * 判断预留金额是否大于发放金额
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkAmountFeeGrant() throws Exception {
		
		FeeGrantService fgs = new FeeGrantService(dao);
		Double d = fgs.checkFeeGrantAmount(feeGrant);
		String data = "ok";
		if(d-feeGrant.getFeeGrantAmount()<0)
			data = d+"";
		request.setAttribute("data", data);
		return toResult();
		
	}
	/**
	 * 添加费用存留发放
	 * @return
	 * @throws Exception
	 */
	public String addFeeGrant() throws Exception
	{
		if(POST())
		{
			String submitType = request.getParameter("submitType");
			HttpServletRequest request = ServletActionContext.getRequest();
			SessionBean sessionBean  = SessionBean.getSessionBean(request);
			feeGrant.setFeeGrantId(UUID.randomUUID().toString());
			feeGrant.setState("在用");
			feeGrant.setCreateUserId(sessionBean.getUserId());
			feeGrant.setCreateUserName(sessionBean.getUserName());
			feeGrant.setCreateDate(new Date());
			
			dao.save(feeGrant);
			if("save".equals(submitType))
			{
				this.Alert_Close("保存成功", "ok");
				return toMessage();
			}
			else
			{
				return toJsp("/jsp/fee/fee_grant/add_fee_grant.jsp");
			}
			
		}
		else
		{
			return toJsp("/jsp/fee/fee_grant/add_fee_grant.jsp");
		}
	}
	/**
	 * 修改费用存留
	 * @return
	 * @throws Exception
	 */
	public String updateFeeGrant() throws Exception
	{
		if(POST())
		{
			String submitType = request.getParameter("submitType");
			dao.update(feeGrant);
			if("save".equals(submitType))
			{
				this.Alert_Close("保存成功", "ok");
				return toMessage();
			}
			else
			{
				return toJsp("/jsp/fee/fee_grant/add_fee_grant.jsp");
			}
		}
		else
		{
			String feeGrantId = request.getParameter("feeGrantId");
			feeGrant = (FeeGrant)dao.findOne(FeeGrant.class, feeGrantId);
			return toJsp("/jsp/fee/fee_grant/update_fee_grant.jsp");
		}
	}
	/**
	 * 删除费用存留
	 * @return
	 * @throws Exception
	 */
	public String deleteFeeGrant() throws Exception
	{
		String feeGrantId = request.getParameter("feeGrantId");
		feeGrant = (FeeGrant)dao.findOne(FeeGrant.class, feeGrantId);
		String data = "ok";
		if(feeGrant.getFeeGrantType().equals("预留"))
		{
			FeeGrantService fgs = new FeeGrantService(dao);
			Double d = fgs.checkFeeGrantAmount(feeGrant);
			if(d-feeGrant.getFeeGrantAmount()<0)
				data = d+"";
		}
		if(data.equals("ok"))
		{
			feeGrant.setState("停用");
			dao.update(feeGrant);
			this.Alert_Close("删除成功", "ok");
		}else{
			this.Alert_Close("存留费用小于发放费用，数据无法被删除！", "ok");
		}
		return toMessage();

	}
	/**
	 * 查看费用存留
	 * @return
	 * @throws Exception
	 */
	public String showFeeGrant() throws Exception
	{
		String feeGrantId = request.getParameter("feeGrantId");
		feeGrant = (FeeGrant)dao.findOne(FeeGrant.class, feeGrantId);
		return toJsp("/jsp/fee/fee_grant/show_fee_grant.jsp");
	}
	public FeeGrant getFeeGrant() {
		return feeGrant;
	}

	public void setFeeGrant(FeeGrant feeGrant) {
		this.feeGrant = feeGrant;
	}

}